package com.dqhl.community.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.model.ExchangeCoupon;
import com.dqhl.community.model.PayMentCoupon;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.ViewHolder;

import java.util.HashMap;
import java.util.List;

/**
 * description: 优惠券Adapter
 * autour: guanluocang
 * date: 2016/10/21 16:35
 */
public class ExchangeCouponAdapter extends BaseAdapter {
    private Context context;
    private List<ExchangeCoupon> exchangeCouponList;
    private LayoutInflater inflater;
    public static HashMap<String, Boolean> states = new HashMap<>();
    private OnRadioButtonCheckedListener onRadioButtonCheckedListener;

    public ExchangeCouponAdapter(Context context, List<ExchangeCoupon> exchangeCouponList,
                                 OnRadioButtonCheckedListener onRadioButtonCheckedListener) {
        this.context = context;
        this.exchangeCouponList = exchangeCouponList;
        inflater = LayoutInflater.from(context);
        this.onRadioButtonCheckedListener = onRadioButtonCheckedListener;
    }

    @Override
    public int getCount() {
        return exchangeCouponList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.lv_item_exchange_coupon, viewGroup, false);
        }
        TableLayout table_layout = ViewHolder.get(view, R.id.table_layout);
        TextView tv_denomination = ViewHolder.get(view, R.id.tv_denomination);
        TextView tv_deno = ViewHolder.get(view, R.id.tv_deno);
        TextView tv_integral = ViewHolder.get(view, R.id.tv_integral);
        TextView tv_coupontime = ViewHolder.get(view, R.id.tv_coupontime);
        final CheckBox cb_coupon = ViewHolder.get(view, R.id.cb_coupon);

        ExchangeCoupon exchangeCoupon = exchangeCouponList.get(i);

        if (exchangeCoupon.getBg() == 0) {
            table_layout.setBackgroundResource(R.drawable.ic_paymentone);
        } else {
            table_layout.setBackgroundResource(R.drawable.ic_paymenttwo);
        }

        tv_denomination.setText(exchangeCoupon.getDenomination());
        tv_deno.setText(exchangeCoupon.getDeno());
        tv_integral.setText(exchangeCoupon.getIntegral());
        tv_coupontime.setText(exchangeCoupon.getTime());

        cb_coupon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (String key : states.keySet()) {
                    states.put(key, false);
                }
                states.put(String.valueOf(i), cb_coupon.isChecked());
                if (cb_coupon.isChecked()) {
                    onRadioButtonCheckedListener.onSelectedPosition(i);
                } else {
                    onRadioButtonCheckedListener.onUnSelectedPosition(i);
                }
                notifyDataSetChanged();
            }
        });
        boolean res = false;
        if (states.get(String.valueOf(i)) == null || !states.get(String.valueOf(i))) {
            res = false;
            states.put(String.valueOf(i), false);
        } else {
            res = true;
        }
        cb_coupon.setChecked(res);


        return view;
    }

    public interface OnRadioButtonCheckedListener {
        void onSelectedPosition(int positon);

        void onUnSelectedPosition(int positon);
    }
}
